package com.zero.controller;

import cn.hutool.core.lang.Console;
import com.alibaba.fastjson.JSON;
import com.zero.input.KafkaMessage;
import com.zero.log.UserscanLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Date;

@Controller
@RequestMapping("DsInfoSJservice")
public class DsInfoService {

    @Autowired
    private KafkaTemplate kafkaTemplate;


    @RequestMapping(value = "webInfoSJService", method = RequestMethod.POST)
    public void webInfoSJService(@RequestBody String jsonstr, HttpServletRequest request, HttpServletResponse response) {
        Console.log("未转换kafkaMessage的数据{}",jsonstr);

        KafkaMessage kafkaMessage=new KafkaMessage();
        kafkaMessage.setJsonmessage(jsonstr);
        kafkaMessage.setCount(1);
        kafkaMessage.setTimestamp(new Date().getTime());
        jsonstr=JSON.toJSONString(kafkaMessage);
        Console.log("转换kafkaMessage后的数据{}",jsonstr);

        //业务开始
        kafkaTemplate.send("test","key",jsonstr);
        //业务结束
        PrintWriter printWriter = getWriter(response);
        response.setStatus(HttpStatus.OK.value());
        printWriter.write("success");
        closeprintwriter(printWriter);
    }


    private PrintWriter getWriter(HttpServletResponse response) {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        OutputStream out = null;
        PrintWriter printWriter = null;
        try {
            out = response.getOutputStream();
            printWriter = new PrintWriter(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return printWriter;
    }

    private void closeprintwriter(PrintWriter printWriter) {
        printWriter.flush();
        printWriter.close();
    }
}
